package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Time;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrdersService ordersService;

    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){

        ordersService.submit(orders);

        return R.success("支付成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, Long number, String beginTime, String endTime){
        log.info("page = {}, pageSize = {}, beginTime = {}, endTime = {}",page,pageSize,beginTime,endTime);
        if (beginTime != null && endTime != null){
            try {
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                LocalDateTime parseBegin = LocalDateTime.parse(beginTime, dateTimeFormatter);
                LocalDateTime parseEnd = LocalDateTime.parse(endTime, dateTimeFormatter);

            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        Page pageOrders = new Page(page,pageSize);

        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.eq(number != null,Orders::getNumber,number);
        lqw.between(beginTime != null && endTime != null,Orders::getOrderTime,beginTime,endTime);

        ordersService.page(pageOrders,lqw);



        return R.success(pageOrders);
    }
}
